package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/10/10 16:43
 **/
public class Day38_1_Num801_MinSwap {
    class Solution {
        public int minSwap(int[] nums1, int[] nums2) {
            int n = nums1.length;
            int[][] f = new int[n][2];
            for(int i = 1; i < n; ++i){
                f[i][0] = f[i][1] = n + 10;
            }
            f[0][1] = 1;
            for(int i = 1; i < n; ++i){
                if(nums1[i] > nums1[i-1] && nums2[i] > nums2[i-1]){
                    f[i][0] = f[i-1][0];
                    f[i][1] = f[i-1][1] + 1;
                }
                if(nums1[i] > nums2[i-1] && nums2[i] > nums1[i-1]){
                    f[i][0] = Math.min(f[i][0], f[i-1][1]);
                    f[i][0] = Math.min(f[i][1], f[i-1][0] + 1);
                }
            }
            return Math.min(f[n-1][0], f[n-1][1]);
        }
    }
}